import json
import requests
import pytest

class TestDemo:

    token = ''
    def test_login(self):
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/flagUser/mobileCodeLogin'
        mobile = 13711110897
        header = {'content-type': 'application/json', 'accept': '*/*'}
        data = {"mobile": mobile, "mobileCode": 888888}
        re = requests.request('post',url=url, headers=header, json=data).json()
        TestDemo.token = re['result']['token']
        print('登录')
        return TestDemo.token

    def test_createflag(self):
        header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization':TestDemo.token}
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/flag/createFlag'
        data = {"flagName":"flagNames","challengeMoney":100,"holidayTime":5,"insistDay":30,"payType":2,"startDate":"2021-08-26 11:32:08","flagUserId":5}
        res = requests.request('post',url=url, headers=header, json=data)
        print(res.json(),'创建flag')
        return res.json()['result']

    def test_paychallengemoney(self):
        header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization':TestDemo.token}
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/pay/setChallengeMoney'
        data = {
            "challengeMoney":100,
            "flagId":self.test_createflag(),
            "payType":2
        }
        res = requests.request('post',url=url, headers=header, json=data)
        print(res.json(),'支付')

    def test_fileupload(self):
        header = {'accept':'*/*','Authorization': TestDemo.token}
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/file/upload?fileType=IMAGE'
        data = {
            "file":('1.jpg',open(r'C:\Users\admin\Desktop\pic\1.jpg','rb'),'image/jpeg')
        }
        res = requests.request('post',url=url,files=data,headers=header)
        return(res.json())

    def test_punchin(self):
        header = {'content-type': 'application/json', 'accept': '*/*', 'Authorization': TestDemo.token}
        url = 'https://hysz-test.huayunshuzi.com/flagAppApi/api/clockInRecord/punchIn'
        print(self.test_createflag())
        data = {
            "content": "接口自动化打卡",
            "flagId":str(self.test_createflag()),
            "pictureList":[self.test_fileupload()],
            "type":3,
            "userId":5
        }
        res = requests.request('post', url=url, json=data, headers=header)
        print(res.json())


if __name__ == '__main__':
    pytest.main(['-vs'])

